package net.sapfree.core.db;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

public class DataBaseType
{
	public static final int DB_UNKNOWN = 0;
	public static final int DB_ORACLE = 1;
	public static final int DB_IBMDB2= 2;
	public static final int DB_MSSQL= 4;
	public static final int DB_ACCESS= 8;
	public static final int DB_MYSQL= 16;
	public static final int DB_SYSBASE= 32;
	public static final int DB_DERBY= 64;

    public static int getConnectionDBType(Connection connection)
    {
    	int iRet = DB_UNKNOWN;
        try
        {
            DatabaseMetaData databasemetadata = connection.getMetaData();
            String s = databasemetadata.getDatabaseProductName();
            iRet = parseDBProductName(s);
        }
        catch(SQLException sqlexception)
        {
            System.out.println("Database error");
        }
        return iRet;
    }

    private static int parseDBProductName(String s)
    {
    	int iRet = DB_UNKNOWN;
        s = s.toUpperCase();
        if(s.indexOf("ORACLE") != -1)
        	iRet = DB_ORACLE;
        else if(s.indexOf("DB2") != -1)
        	iRet = DB_IBMDB2;
        else if(s.indexOf("ACCESS") != -1)
        	iRet = DB_ACCESS;
        else if(s.indexOf("MYSQL") != -1)
        	iRet = DB_MYSQL;
        else if(s.indexOf("SQL") != -1 )
        	iRet = DB_MSSQL;
        else if(s.indexOf("DERBY") != -1)
        	iRet = DB_DERBY;
        return iRet;
    }
}
